
import { PageModalService } from './../../shared/service/page-modal-service';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from '../../shared/service/http.service';
import { ActivatedRoute, Router } from '@angular/router';
import { Component, Input, OnInit } from '@angular/core';
import { ActionSheetService } from 'ng-zorro-antd-mobile';
//tslint:disable

@Component({
  selector: 'app-ad-sh',
  templateUrl: './ad-sh.component.html',
  styleUrls: ['./ad-sh.component.scss'],
})
export class AdShComponent implements OnInit {
  @Input() inviteCode;
  @Input() clientType;
  @Input() buttonlinks;
  @Input() btnTitle;
  @Input() tagData;
  @Input() supplierLink;

  user: any;
  code: any;
  staffType = [];
  isApp = false;
  constructor(
    private router: Router,
    private httpService: HttpService,
    private _actionSheet: ActionSheetService, // 必须有
    private route: ActivatedRoute,
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
  ) { }

  ngOnInit() {
    if (localStorage.getItem('client') === 'APP') {
      this.isApp = true;
    } else {
      this.isApp = false;
    }

    this.buttonlinks.forEach(element => {
      if (element.link && element.link.params && element.link.params.h5) {
        this.staffType.push({ name: element.link.params.label, value: element.link.params.value });
      }
    });

    this.route.queryParams.subscribe(p => {
      if (p.code) {
        this.code = p.code;
      } else {
        this.code = '0';
      }
      this.httpService.get('userRegister/dataInfo/1/' + this.code, res => {
        if (res.code === 0) {
          if (res.user != null) {
            this.user = res.user;
          } else {
          }
          // this.staffType = res.staffType;
        } else {
          this.appGlobalService.showToast(res.msg, 3000, 'top');
        }
      });
    });
    // 524250
  }
  onClick(index) {
    if (this.user && this.user.name) {
      console.log(index);
      this.router.navigate(['/register-sh'], {
        queryParams: { code: this.code, index, staffType: JSON.stringify(this.staffType), user: JSON.stringify(this.user) }
      });
    } else {
      this.router.navigate(['/register-sh'], {
        queryParams: { code: this.code, index, staffType: JSON.stringify(this.staffType) }
      });
    }

  }

  showActionSheet = (selectIndex) => {
    // 申请类型（身份类型）：1全职员工，3兼职员工，5个人代理(工人、设计师、房管家、直租直售)，7团队代理（服务店、建材销售），9城市运营商

    if (!this.buttonlinks) { return; }
    const that = this;
    // let lastIndex: -1;
    const buttons: any = [];
    this.buttonlinks.forEach((element, index) => {
      buttons.push(element.link.params.label);
      // if (this.register.typeName === element.name) {
      // lastIndex = index;
      // }
    });

    ActionSheetService.showActionSheetWithOptions(
      {
        options: buttons,
        // cancelButtonIndex: buttons.length - 1,
        destructiveButtonIndex: selectIndex,
        title: this.btnTitle,
        // message: message,
        maskClosable: true
      },
      buttonIndex => {
        try {
          if (buttonIndex <= -1) {
            return;
          }
          const link = this.buttonlinks[buttonIndex].link;
          if (link.params && link.params.h5) {
            if (this.user && this.user.name) {
              this.router.navigate(['/register-sh'], {
                queryParams: { code: this.code, index: selectIndex, staffType: JSON.stringify(this.staffType), user: JSON.stringify(this.user) }
              });
            } else {
              for (let i = 0; i < this.staffType.length; i++) {
                // _this.buttonlinks[selectIndex].link.params.label
                const temp = this.staffType[i];
                if (temp.name === this.buttonlinks[buttonIndex].link.params.label) {
                  selectIndex = i;
                  break;
                }
              }
              this.router.navigate(['/register-sh'], {
                queryParams: { code: this.code, index: selectIndex, staffType: JSON.stringify(this.staffType) }
              });
            }
          } else {
            const pageParams = {
              pageId: link.target,
              params: link.params ? link.params : {},
              sessionId: localStorage.getItem('sessionId')
            };
            this.appGlobalService.goToForward(pageParams);
          }

        } catch (error) {

        }

      }
    );
  }
  phone(phone) {
    this.httpService.changeCallNumber(phone);
  }
  supplierClick() {
    this.pageModalService.openPage(this.supplierLink);
  }

  openPage() {
    // http://localhost:8100/ad-new5?obj={"code":451058,"inviteKey":"de1ba728351f80c5f6964ca135b4dad7","status":4}
    try {
      let url = this.tagData.data.newLinkUrl
        + '?obj={"code":' + parseInt(this.tagData.data.newLinkUrlCode)
        + ',"inviteKey":"'
        + this.tagData.data.newLinkUrlKey
        + '","status":'
        + parseInt(this.tagData.data.newLinkUrlStatus);
      if (this.tagData.data.hideInviteMan !== undefined) {
        url += ',"hideInviteMan":1}';
      } else {
        url += '}';
      }
      // '}'
      this.appGlobalService.openUrl2(this.tagData.data.newLinkTitle, url, this.buttonlinks[0]);
    } catch (error) {

    }

  }
}
